/**
 * 
 */
package com.roc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import com.roc.dao.DaoException;
import com.roc.dao.IPagenitionDao;
import com.roc.page.BaseSearchCondition;
import com.roc.util.ApplicationContextUtils;

/**
 * @author Administrator
 *
 */
@Repository
public class PageniionDaoImpl implements IPagenitionDao {
	private Logger logger = LogManager.getLogger(PageniionDaoImpl.class);
	@Autowired
	SessionFactory sessionFactory;

	/* (non-Javadoc)
	 * @see com.roc.dao.IPagenitionDao#findAll(com.roc.page.BaseSearchCondition, int, int)
	 */
	
	@SuppressWarnings("unchecked")
	public List<Object> findAll(BaseSearchCondition searchCondition,
			int startRow, int maxRow) throws DaoException {
		List<Object> searchRsList=new ArrayList<Object>();
		try{
			String hql=searchCondition.getFullSearchHQL(null);
			Session session=sessionFactory.getCurrentSession();
			Query query=session.createQuery(hql);
			List<Object> params=new ArrayList<Object>();
			params=searchCondition.getParams();
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++)
				  query.setParameter(i,params.get(i));
			}
			query.setFirstResult(startRow);
			query.setMaxResults(maxRow);
			searchRsList=query.list();
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query recordes error",e);
		}
		return searchRsList;
	}


	/* (non-Javadoc)
	 * @see com.roc.dao.IPagenitionDao#count(com.roc.page.BaseSearchCondition)
	 */
	public int count(BaseSearchCondition searchCondition) throws DaoException {
		long rowCount=0;
		try{
			String hql=searchCondition.getFullRowCountHQL(null);
			Session session=sessionFactory.getCurrentSession();
			Query query=session.createQuery(hql);
			List<Object> params=new ArrayList<Object>();
			params=searchCondition.getParams();
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++)
				  query.setParameter(i,params.get(i));
			}
			rowCount=(Long) query.uniqueResult();
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query total recordes error",e);
		}
		return (int)rowCount;
	}
	@SuppressWarnings("unchecked")
	public List<Object> findAllSql(BaseSearchCondition searchCondition,
			int startRow, int maxRow) throws DaoException {
		List<Object> searchRsList=new ArrayList<Object>();
		try{
			String sql=searchCondition.getFullSearchHQL(null);
			Session session=sessionFactory.getCurrentSession();
			Query query=session.createSQLQuery(sql);
			List<Object> params=new ArrayList<Object>();
			params=searchCondition.getParams();
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++)
				  query.setParameter(i,params.get(i));
			}
			query.setFirstResult(startRow);
			query.setMaxResults(maxRow);
			searchRsList=query.list();
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query recordes error",e);
		}
		return searchRsList;
	}
	
	@Override
	public int countSql(BaseSearchCondition searchCondition)
			throws DaoException {
		int rowCount=0;
		try{
			String sql=searchCondition.getFullRowCountHQL(null);
		
			Session session=sessionFactory.getCurrentSession();
			
			Query query=session.createSQLQuery(sql);
			List<Object> params=new ArrayList<Object>();
			params=searchCondition.getParams();
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++){
					query.setParameter(i,params.get(i));
				
				
				}
			}
			String str= (String)query.uniqueResult().toString();
			if(str !=null){
				rowCount = Integer.parseInt(str);
			}
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query total recordes error",e);
		}
		return rowCount;
	}
	
	public List<Object> findAllByParams(String sql,List<Object> params,
			int startRow, int maxRow) throws DaoException {
		List<Object> searchRsList=new ArrayList<Object>();
		try{
			Session session=sessionFactory.getCurrentSession();
			Query query=session.createSQLQuery(sql);
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++)
				  query.setParameter(i,params.get(i));
			}
			query.setFirstResult(startRow);
			query.setMaxResults(maxRow);
			searchRsList=query.list();
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query recordes error",e);
		}
		return searchRsList;
	}
	
	@Override
	public int countByParams(String sql,List<Object> params)
			throws DaoException {
		int rowCount=0;
		try{
			Session session=sessionFactory.getCurrentSession();
			Query query=session.createSQLQuery(sql);
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++){
					query.setParameter(i,params.get(i));
				
				
				}
			}
			String str= (String)query.uniqueResult().toString();
			if(str !=null){
				rowCount = Integer.parseInt(str);
			}
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query total recordes error",e);
		}
		return rowCount;
	}
	

}
